package com.dycx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.system.dto.ConfigDTO;
import com.dycx.system.entity.ConfigDO;
import com.dycx.system.mapper.ConfigMapper;
import com.dycx.system.service.ConfigService;
import com.dycx.system.service.OssFileService;
import com.dycx.system.vo.ConfigVO;
import com.dycx.system.vo.SystemConfig;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
@AllArgsConstructor
public class ConfigImpl extends ServiceImpl<ConfigMapper, ConfigDO> implements ConfigService {

    private final ConfigMapper configMapper;
    private final OssFileService ossFileService;
    private final StringRedisTemplate stringRedisTemplate;

    public static final String REDIS_SYSTEM_CONFIG_KEY = "SystemConfigKey:";

    /**
     * 获取所有系统配置
     *
     * @return
     */
    @Override
    public List<ConfigVO> getAllConfig() {
        List<ConfigVO> list = configMapper.getConfigType();
        list.forEach(item -> {
            LambdaQueryWrapper<ConfigDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ConfigDO::getTypeId, item.getTypeId());
            List<ConfigDO> cList = this.list(queryWrapper);
            cList.forEach(o -> {
                if (o.getInputType().equals("password")) {
                    o.setValue("******************************");
                }

                if (o.getInputType().equals("image")) {
                    o.setValue(AliyunOss.addDomain(o.getValue()));
                }
            });

            item.setList(cList);
        });

        return list;
    }

    /**
     * 修改系统配置
     *
     * @param configDTOList
     * @return
     */
    @Override
    public Boolean updateConfig(List<ConfigDTO> configDTOList) {
        configDTOList.forEach(item -> {
            item.getList().forEach(config -> {
                if (!"******************************".equals(config.getValue())) {
                    ConfigDO oldEntity = this.getById(config.getId());

                    ConfigDO configDO = new ConfigDO();
                    configDO.setId(config.getId());

                    if (config.getInputType().equals("image")) {
                        configDO.setValue(AliyunOss.delDomain(config.getValue()));
                    } else {
                        configDO.setValue(config.getValue());
                    }

                    if (this.updateById(configDO)) {
                        if (config.getInputType().equals("image")) {
                            ossFileService.setDeleted(oldEntity.getValue());
                            ossFileService.setUse(configDO.getValue());
                        }
                    }
                }
            });
        });

        stringRedisTemplate.delete(REDIS_SYSTEM_CONFIG_KEY);
        this.getSystemConfig();

        return true;
    }

    /**
     * 获取配置并转为对象
     */
    @Override
    public SystemConfig getSystemConfig() {
        String redisSystemConfigStr = stringRedisTemplate.opsForValue().get(REDIS_SYSTEM_CONFIG_KEY);
        if (StrUtil.isNotBlank(redisSystemConfigStr)) {
            return JSON.parseObject(redisSystemConfigStr, SystemConfig.class);
        }

        List<ConfigDO> list = this.list();
        HashMap<String, Object> map = new HashMap<>();
        list.forEach(item -> {
            String newValue = item.getInputType().equals("image") ? AliyunOss.addDomain(item.getValue()) : item.getValue();
            map.put(StrUtil.toCamelCase(item.getKey()), newValue);
        });

        SystemConfig config = BeanUtil.toBean(map, SystemConfig.class);
        stringRedisTemplate.opsForValue().set(REDIS_SYSTEM_CONFIG_KEY, JSON.toJSONString(config));

        return config;
    }

}
